#ifndef __CSCRIPT__H__
#define __CSCRIPT__H__

#include "ResourceInputStream.h"

#define ENABLE_CMD_DEFAULT_PARAM 1
#define MAX_CMD_PARAMS 16

// Must match values in exporter
#define NUM_BYTES_FOR_COMMAND 2
#define NUM_BYTES_FOR_PARAM_TYPE 2
#if ENABLE_CMD_DEFAULT_PARAM
#define FIRST_PARAM_OFFSET 5 // NUM_BYTES_FOR_COMMAND + NUM_BYTES_FOR_PARAM_TYPE + 1 byte for num params
#else
#define FIRST_PARAM_OFFSET 4 // NUM_BYTES_FOR_COMMAND + NUM_BYTES_FOR_PARAM_TYPE
#endif

// Must match values in exporter
#define OP_LBR -32768
#define OP_RBR -32767
#define OP_ADD -32766
#define OP_SUB -32765
#define OP_MUL -32764
#define OP_DIV -32763
#define OP_LAR -32762
#define OP_LEQ -32761
#define OP_SMA -32760
#define OP_SEQ -32759
#define OP_EQU -32758
#define OP_NEQ -32757

class CScript
{
private:
	static UINT8** m_commandHeader;
	static INT16 m_numCommands;
	static t_var* m_vars;

	INT32 m_cursorPos;
	INT32 m_scriptLength;
	UINT8* m_scriptData;

	// Current command
	INT32* m_crtCommandParams;
	void InitCrtCommand();

public:

	// Command processing
	t_timer m_waitTime;
#if defined(WIN32) || defined(ANDROID_OS) || defined(BADA_OS)
	time_t m_clockStartTime;
#elif defined(IPHONE)
	t_timer m_clockStartTime;
#endif
	
	// Keys
	INT32 m_labelID;

	static CScript* LoadScript(UINT32 resID);
	static void Init(UINT32 cmd_header_res_id);
	static void Destroy();

	CScript(ResourceInputStream *buf, int size);
	~CScript();

	// Seeking
	void Forward(INT16 offset = 1);
	void GotoLabel(UINT8 labelID);
	void GotoElse();
	void GotoEndif();
	bool IsEnded();
	// TODO
	//void SetCursorPos(UINT16 pos);
	//UINT16 GetCursorPos();

	// Command
	UINT16 GetCommand();
	UINT16 GetNextCommand();
	INT32 GetParam(UINT8 index);
	INT32 GetParamValue(UINT8 index);
	INT32* GetParams() {return m_crtCommandParams;}
	UINT8 GetCrtCommandNumParams();
	UINT8 GetCrtCommandSize();
	bool IsParamVar(UINT8 index);

	static void SetVar(UINT16 index, t_var val);
	static t_var GetVar(UINT16 index);
	static UINT8 GetCommandNumParams(UINT16 cmd);
	static UINT8 GetCommandSize(UINT16 cmd);
	static UINT8 GetParamSize(UINT16 cmd, UINT8 index);
#if ENABLE_CMD_DEFAULT_PARAM
	static INT32 GetDefaultParam(UINT16 cmd, UINT8 index);
#endif
};


#endif // __CSCRIPT__H__
